<?php
include '../../../wp-blog-header.php';
require_once('rss_fetch.inc');
$url = 'http://www.sosasta.com/rss_buzzr.php';
$rss = fetch_rss($url);
$brand_id = '286';

/*
echo '<pre>';
print_r($rss->items);
echo '</pre>';
exit();
*/

//$i=0;
foreach($rss->items as $key=>$value)
{ 
  date_default_timezone_set('Asia/Kolkata');
  if(strtotime($value['end_date']) >= strtotime(date('Y-m-d')))
  {
    $columns_deal['name'] = pods_sanitize($value['title']);
    $columns_deal['camp_link'] = substr(sanitize_title($columns_deal['name']),0,128);
    $ext_img_url =$value['image_url'];
     
    $deal = new Pod('deal');
    $where = 't.camp_link = "'.$columns_deal['camp_link'].'" OR (t.ext_image_url = "'.$ext_img_url.'" AND t.daily = "1")';
    $deal->findRecords('id DESC', 1,$where);
    $dealsFound = $deal->getTotalRows();
    if($dealsFound == 0)
    {
      //echo $i.' : '.$value['title']."<br />";
      //$i++;
      if($value['city_name'] == 'Delhi/NCR')
      {
        $value['city_name'] = 'Delhi';
      }
      
      $city_name = $value['city_name'];
      $city = new Pod('city');
    	$city->findRecords("name ASC",1,"t.name = '$city_name'");
    	while($city->fetchRecord())
    	{
      	$city_id  = $city->get_field('id');
    	}
    
      $columns_deal['city'] = $city_id;
      //$columns_deal['short'] = $value['description'];
      
      $shortened_url = make_bitly_url($value['offer_url']);
      if(empty($shortened_url))
      {
        $shortened_url = make_goog_url($value['offer_url']);
      }
      $columns_deal['extlink'] = $shortened_url;
      
      //$columns_deal['extlink'] =  make_bitly_url($value['offer_url']);
      $columns_deal['camp_end'] = date('Y-m-d 23:59:00',strtotime('+5 day'));
      $columns_deal['camp_start'] = date('Y-m-d H:i:s');
      $columns_deal['brand'] = $brand_id;
      $columns_deal['daily'] = '1';
      $columns_deal['oriprice'] = floor($value['actual_price']);
      $columns_deal['disper'] = floor($value['discount']);
      $columns_deal['disprice'] = floor($value['offer_price']);
      $columns_deal['ext_image_url'] = $value['image_url'];
    
      $cat_id = check_daily_category($columns_deal['name']);
      if(!empty($cat_id))
      {
        $columns_deal['camp_cat'] = $cat_id;
      }
    
      $api = new PodAPI();
      $params = array('datatype' => 'deal', 'columns' => $columns_deal);
      
      /*
      echo '<pre>';
      print_r($params);
      echo '</pre>';
      exit();
      */
      
      $api->save_pod_item($params);
    }
  }
}